package
{
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class Formatting
	{
		[Embed(source="assets/fonts/Cookies.ttf",
		fontFamily="Cookies",
		fontName = "Cookies", 
	    mimeType = "application/x-font", 
	    fontWeight="normal", 
	    fontStyle="normal", 
	    advancedAntiAliasing="true", 
	    embedAsCFF="false")]
		private static var Cookies:Class;
		
		private static var init:Boolean = false;
		
		public static var greyscaleFilter:ColorMatrixFilter;
		
		public function Formatting(){
		}
		
		/**
		 * Must be called to initialize the 'Cookies' font before any text is created.
		 */
		public static function setup():void{
			if(init) return;
			Font.registerFont(Cookies);
			var mat:Array = [ .33,.33,.33,0,0,
				.33,.33,.33,0,0,
				.33,.33,.33,0,0,
				.33,.33,.33,1,0 ];
			greyscaleFilter = new ColorMatrixFilter(mat);
			init = true;
		}
		
		/**
		 * Returns a new format with the Cookies font. (Size 30 for titles, 22 for subtitles)
		 * @param pt: the font size
		 */
		public static function getFormat(pt:int):TextFormat{
			var tf : TextFormat = new TextFormat();
			tf.font = "Cookies";
			tf.color = 0xFFFFFF;
			tf.size	= pt;
			return tf;
		}
		
		/**
		 * Returns a new format with the Cookies font. (width 1.5 for titles, 1.2 for subtitles)
		 * @param thickness: the width of the outline
		 */
		public static function getOutlineFilter(thickness:Number):Array{
			var outline:GlowFilter = new GlowFilter();
			outline.blurX = outline.blurY = thickness;
			outline.color = 0x000000;
			outline.quality = BitmapFilterQuality.HIGH;
			outline.strength = 50;
			var a : Array = new Array(outline);
			return a;
		}
		
		public static function newSubtitle(text:String, color:uint=0xFFFFFF, x:int=0, y:int=0, wid:int=100, hgt:int=100):TextField{
			var txt : TextField = new TextField();
			
			txt.border = false;
			txt.wordWrap = false;
			txt.embedFonts = true;
			txt.antiAliasType = AntiAliasType.NORMAL;
			txt.selectable = false;
			txt.autoSize = TextFieldAutoSize.LEFT;
			
			//define font
			var t : TextFormat = getFormat(22);
			t.color = color;
			txt.defaultTextFormat = t;
			
			//define outline filter
			txt.filters = Formatting.getOutlineFilter(1.2);
			
			txt.x = x;
			txt.y = y;
			txt.width = wid;
			txt.height = hgt;
			txt.text = text;
			
			return txt;
		}
		
		public static function newTitle(text:String, color:uint=0xFFFFFF, x:int=0, y:int=0, wid:int=100, hgt:int=100):TextField{
			var txt : TextField = new TextField();
			
			txt.border = false;
			txt.wordWrap = false;
			txt.embedFonts = true;
			txt.antiAliasType = AntiAliasType.NORMAL;
			txt.selectable = false;
			txt.autoSize = TextFieldAutoSize.LEFT;
			
			//define font
			var t : TextFormat = getFormat(30);
			t.color = color;
			txt.defaultTextFormat = t;
			
			//define outline filter
			txt.filters = Formatting.getOutlineFilter(1.5);
			
			txt.x = x;
			txt.y = y;
			txt.width = wid;
			txt.height = hgt;
			txt.text = text;
			
			return txt;
		}
	}
}